package net.oov.identify.support;

import java.io.IOException;
import java.util.List;

import net.oov.identify.Buck;
import net.oov.identify.OI;
import net.oov.identify.SequenceFreqContainer;
import net.oov.identify.SequenceFreqContainerFactory;
import net.oov.identify.model.CheckExtModel;
import net.oov.identify.model.ConfModel;
import net.oov.identify.model.FreqModel;
import net.oov.identify.model.LengthModel;
import net.oov.identify.model.MutualInfoExtModel;
import net.oov.identify.model.MutualInfoModel;
import net.oov.identify.model.NGramModel;
import net.oov.identify.model.StatusNode;

import org.apache.lucene.analysis.Analyzer;
import org.wltea.analyzer.lucene.IKAnalyzer;


public class WordDiscerner {
	
	//决策模型常量
	public static final int[] WORD_LENGTH_VALID = new int[]{3};//{2,3}
	public static final Integer CHARSEQUENCE_MIN_FREQ = 2;
	public static final Double CHARSEQUENCE_MIN_MI = 2.0d;
	public static final Double CHARSEQUENCE_MI_DEFERENCE_VALUE = 0.5d;
	public static final Double CHARSEQUENCE_MIN_CONF = 0.01d;
	public static final Double CHARSEQUENCE_MIN_NGRAM = 0.15d;
	public static final int CHARSEQUENCE_NUM_OF_SENTENTCE = 56000;
	
	public static final Integer CHARSEQUENCE_MIN_FREQ_WHOLE = 0;
	
	
	private final SequenceFreqContainer initDic(int loadMethod){
		String dirName = "taobao_products";
//		String dirName2 = "taobao_products2";
		List<String> rawTextPathIn = CorpusManager.getCorpusTextPath(dirName);
//		List<String> rawTextPathIn2 = CorpusManager.getCorpusTextPath(dirName2);
//		rawTextPathIn.addAll(rawTextPathIn2);
		
		Analyzer analyzer = new IKAnalyzer();//new IKAnalyzer();new ChineseAnalyzer();
		SequenceFreqContainerFactory cf = new SequenceFreqContainerFactory();
		SequenceFreqContainer scr = cf.createNewContainer(rawTextPathIn, analyzer, loadMethod, WORD_LENGTH_VALID[0]+1);
		
		return scr;
	}
	
	/**
	 * @param cr
	 * @return
	 */
	public final CheckExtModel getModel2(SequenceFreqContainer cr){
		
		CheckExtModel cxm = new CheckExtModel();
		LengthModel mlm = new LengthModel(WORD_LENGTH_VALID);
		FreqModel fm = new FreqModel(0, 1);
		
		cxm.addModel(mlm);
		cxm.addModel(fm);
		
		return cxm;
		
	}
	
	public final CheckExtModel getModel1(SequenceFreqContainer cr){
		
		Buck buck = new Buck(cr.getDicForward());
		CheckExtModel cxm = new CheckExtModel();
		LengthModel mlm = new LengthModel(WORD_LENGTH_VALID);
		FreqModel fm = new FreqModel(CHARSEQUENCE_MIN_FREQ, CHARSEQUENCE_MIN_FREQ_WHOLE);
		MutualInfoModel mim = new MutualInfoModel(buck, CHARSEQUENCE_MIN_MI, CHARSEQUENCE_NUM_OF_SENTENTCE);
		MutualInfoExtModel mix = new MutualInfoExtModel(cr, CHARSEQUENCE_MI_DEFERENCE_VALUE, mim);
		
		ConfModel cm = new ConfModel(CHARSEQUENCE_MIN_CONF, buck);
		NGramModel ngm = new NGramModel(buck, CHARSEQUENCE_MIN_NGRAM);
		
		cxm.addModel(mlm);
		cxm.addModel(fm);
		cxm.addModel(cm);
		cxm.addModel(mim);
		cxm.addModel(mix);
		cxm.addModel(ngm);
		
		return cxm;
		
	}
	
	public static void main(String[] args) throws IOException{
		
		WordDiscerner wd = new WordDiscerner();
		SequenceFreqContainer cr = wd.initDic(SequenceFreqContainer.LOAD_METHOD_BOTH);
		String outFile = "txt/out_word.txt";	
		CheckExtModel cxm = wd.getModel1(cr);
		cxm.out2File(cr, outFile);
		System.out.println("finished");
	}
	
	
	/**
	 * 查看字串的各类决策值
	 * @param str
	 */
	public void showWordInfo(String str){
		
		SequenceFreqContainer cr = initDic(SequenceFreqContainer.LOAD_METHOD_FORWARD);
		
		Buck buck = new Buck(cr.getDicForward());
		CheckExtModel cxm = new CheckExtModel();
		FreqModel fm = new FreqModel(CHARSEQUENCE_MIN_FREQ);
		MutualInfoModel mim = new MutualInfoModel(buck, CHARSEQUENCE_MIN_MI, CHARSEQUENCE_NUM_OF_SENTENTCE);
		ConfModel cm = new ConfModel(CHARSEQUENCE_MIN_CONF, buck);
		NGramModel ngm = new NGramModel(buck, CHARSEQUENCE_MIN_NGRAM);
		cxm.addModel(fm);
		cxm.addModel(cm);
		cxm.addModel(mim);
		cxm.addModel(ngm);
		
		OI.w("freq:");
		for(int i=0;i<str.length();i++){
			OI.w(buck.findTreeNode(str.substring(i,i+1)).freq()+",");
		}
		StatusNode ct = buck.findTreeNode(str);
		OI.wl("通过："+cxm.decision(ct,str));
		OI.wl(cxm.info());
		
	}
	
}
